from django import forms
from django.core.exceptions import ValidationError
from django.utils.translation import gettext_lazy as _
import datetime
from .models import Book

class RenewBookForm(forms.Form):
    renewal_date = forms.DateField(
        help_text="请输入一个介于今天和4周内的日期（默认3周）",
        widget=forms.TextInput(attrs={'type': 'date'})
    )

    def clean_renewal_date(self):
        data = self.cleaned_data['renewal_date']
        if data < datetime.date.today():
            raise ValidationError(_('无效日期 - 续借日期不能早于今天'))
        if data > datetime.date.today() + datetime.timedelta(weeks=4):
            raise ValidationError(_('无效日期 - 续借日期不能超过4周'))
        return data

class BookForm(forms.ModelForm):
    class Meta:
        model = Book
        fields = '__all__'